package com.ty.admin.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ty.common.constant.ResultMsgEnum;
import com.ty.common.validator.group.SaveGroup;
import com.ty.common.validator.group.UpdateGroup;
import com.ty.skysystem.entity.ContactEntity;
import com.ty.skysystem.entity.TruckEntity;
import com.ty.skysystem.entity.req.DriverEntityREQ;
import com.ty.skysystem.entity.req.TruckEntityREQ;
import com.ty.skysystem.entity.vo.TruckEntityVo;
import com.ty.skysystem.service.ContactService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.NamedBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.ty.skysystem.entity.DriverEntity;
import com.ty.skysystem.service.DriverService;
import com.ty.common.utils.PageUtils;
import com.ty.common.utils.R;

/**
 * 驾驶员信息表: 用于存放和管理驾驶员信息 字段列: team_id => 对应表: truck_team
 *
 * @author tianyuzhu
 * @email 1659770218@QQ.COM
 * @date 2020-12-11 15:40:53
 */
@RestController
@RequestMapping("driver")
@Api(tags = "驾驶员信息管理模块: 用于存放和管理驾驶员信息")
@Slf4j
public class DriverController {
  @Autowired private DriverService driverService;

  @Autowired private ContactService contactService;

  /** 列表 */
  @GetMapping("/list")
  @ApiOperation(value = "查询全部驾驶员信息", notes = "查询全部驾驶员信息", response = DriverEntity.class)
  public R list(
      @ApiParam(value = "参数", name = "Map<String, Object> params") @RequestParam
          Map<String, Object> params) {
    PageUtils page = driverService.queryPage(params);

    return R.ok().put(page);
  }

  /** 信息 */
  @GetMapping("/info/{driverId}")
  @ApiOperation(value = "查询单个驾驶员信息", notes = "查询单个驾驶员信息", response = DriverEntity.class)
  public R info(
      @ApiParam(value = "需要查询的id,从URI获取", name = "Long driverId") @PathVariable("driverId")
          Long driverId) {
    DriverEntity driver =
        this.driverService.getOne(
            new QueryWrapper<DriverEntity>()
                .eq("driver_id", driverId)
                .select(DriverEntity.class, e -> !"is_deleted".equals(e.getColumn())));
    return R.ok().put(driver);
  }

  /** 保存 */
  @PostMapping("/save")
  @ApiOperation(value = "保存驾驶员信息", notes = "保存驾驶员信息")
  public R save(
      @ApiParam(value = "驾驶员实体类", name = "DriverEntity driver")
          @Validated(SaveGroup.class)
          @RequestBody
          DriverEntity driver) {
    return driverService.save(driver)
        ? R.ok(ResultMsgEnum.INSERT_OK.getMsg())
        : R.error(ResultMsgEnum.INSERT_ERROR.getMsg());
  }

  /** 修改 */
  @PatchMapping("/update")
  @ApiOperation(value = "修改驾驶员信息", notes = "修改驾驶员信息")
  public R update(
      @ApiParam(value = "驾驶员实体类", name = "DriverEntity driver")
          @Validated(UpdateGroup.class)
          @RequestBody
          DriverEntity driver) {
    driver.setUpdateTime(new Date());
    return driverService.updateById(driver)
        ? R.ok(ResultMsgEnum.UPDATE_OK.getMsg())
        : R.error(ResultMsgEnum.UPDATE_ERROR.getMsg());
  }

  /** 删除 */
  @DeleteMapping("/delete")
  @ApiOperation(value = "删除驾驶员,id数组,逻辑删除", notes = "删除驾驶员,id数组,逻辑删除")
  public R delete(
      @ApiParam(value = "需要删除的ID数组", name = "Long[] driverIds") @RequestBody Long[] driverIds) {
    if (driverIds.length == 0) {
      return R.error("ID不能为空");
    }
    return driverService.removeByIds(Arrays.asList(driverIds))
        ? R.ok(ResultMsgEnum.DELETE_OK.getMsg())
        : R.error(ResultMsgEnum.DELETE_ERROR.getMsg());
  }

  /** 驾驶员车辆绑定 */
  @PostMapping("/driverTruckBind")
  @ApiOperation(value = "驾驶员车辆绑定", notes = "驾驶员车辆绑定")
  public R driverTruckBind(
      @ApiParam(value = "车辆司机关联实体类", name = "ContactEntity contactEntity")
          @Validated(SaveGroup.class)
          @RequestBody
          ContactEntity contactEntity) {
    return contactService.save(contactEntity)
        ? R.ok(ResultMsgEnum.BIND_OK.getMsg())
        : R.error(ResultMsgEnum.BIND_ERROR.getMsg());
  }

  /** 驾驶员车辆解绑 */
  @DeleteMapping("/driverTruckUntie")
  @ApiOperation(value = "驾驶员车辆解绑", notes = "驾驶员车辆解绑")
  public R driverTruckUntie(@RequestBody Long[] contactIds) {
    if (contactIds.length == 0) {
      return R.error("ID不能为空");
    }
    return contactService.removeByIds(Arrays.asList(contactIds))
        ? R.ok(ResultMsgEnum.UNTIE_OK.getMsg())
        : R.error(ResultMsgEnum.UNTIE_ERROR.getMsg());
  }

  /** 查询所有未绑定车辆的驾驶员 */
  @GetMapping("/driversWithoutBoundVehicles")
  @ApiOperation(value = "查询所有未绑定车辆的驾驶员", notes = "查询所有未绑定车辆的驾驶员", response = DriverEntity.class)
  public R driversWithoutBoundVehicles(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "limit", defaultValue = "5") Integer limit,
      DriverEntityREQ driverEntityREQ) {
    PageUtils pageUtils =
        driverService.selectDriversWithoutBoundVehicles(page, limit, driverEntityREQ);
    return R.ok().put(pageUtils);
  }

  /** 查询所有未绑定驾驶员的车辆 */
  @GetMapping("/vehicleWithoutDriverBound")
  @ApiOperation(value = "查询所有未绑定驾驶员的车辆", notes = "查询所有未绑定驾驶员的车辆", response = TruckEntity.class)
  public R vehicleWithoutDriverBound(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "limit", defaultValue = "5") Integer limit,
      TruckEntityREQ truckEntityREQ) {
    PageUtils pageUtils =
        driverService.selectVehicleWithoutDriverBound(page, limit, truckEntityREQ);
    return R.ok().put(pageUtils);
  }

  /**
   * 根据驾驶员ID查出已经绑定的多个车辆
   */
  @GetMapping("selectTruckIsDriverId/{id}")
  @ApiOperation(value = "根据驾驶员ID查出已经绑定的多个车辆", notes = "根据驾驶员ID查出已经绑定的多个车辆", response = TruckEntityVo.class)
  public R selectTruckIsDriverId(@ApiParam("驾驶员ID,根据URI获取") @PathVariable("id") Integer driverId){
    return R.ok().put(driverService.selectTruckIsDriverId(driverId));
  }

}
